package com.wzk.travelspringboot.controller;

import com.wzk.travelspringboot.dto.Result;
import com.wzk.travelspringboot.utils.MinioUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @Author: WangZiKang
 * @Date: 2025/1/7 22:02
 */
@Slf4j
@RestController
@RequestMapping("/file")
@RequiredArgsConstructor
public class UploadFileController {

    private final MinioUtil minioUtil;

    @PostMapping("/upload")
    public Result upload(MultipartFile file) throws IOException {
        //1.判断文件类型是否合法
        boolean b = minioUtil.checkFileSuffix(file.getOriginalFilename());
        if (!b){
            return Result.failed("文件不合法！");
        }
        //2.上传文件并返回浏览路径
        String fileUrl = minioUtil.uploadImageFile("avatar", file.getOriginalFilename(), file.getInputStream());
        return Result.success("上传成功！",fileUrl);
    }

}
